﻿#region Copyright

// /*
// SilverlightInEnterprise
// SilverlightInEnterprise
// ActiveScreensViewModel.cs
// 
// Author: Matt Van Horn
// Created 11/15/2011 7:28 PM
// Copyright: Silverlight in the Enterprise www.slinenterprise.com
// 
// Licensed under MS-PL http://www.opensource.org/licenses/MS-PL and while you 
// may use this in anyway you see fit and owe us nothing to do so, we wouldn't mind knowing that you got value out of the work we did (so we will do more) so 
// we ask that you leave a comment on www.slinenterprise.com or on the codeplex site slinenterprise.codeplex.com
// 
// */

#endregion

using System;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using SilverlightInEnterprise.Common.Events;
using SilverlightInEnterprise.Common.ViewModels;
using SilverlightInEnterprise.Interfaces.Screens;
using SilverlightInEnterprise.Modules.Case.ViewModels;

namespace SilverlightInEnterprise.ViewModels
{
    public class ActiveScreensViewModel : ViewModelBase, IPartImportsSatisfiedNotification
    {
        private readonly ObservableCollection<IScreen> screens = new ObservableCollection<IScreen>();
        private readonly ReadOnlyObservableCollection<IScreen> readOnlyScreens;

        public ReadOnlyObservableCollection<IScreen> Screens
        {
            get { return this.readOnlyScreens; }
        }

        #region SelectedScreenIndex Property

        private int _SelectedScreenIndex;

        public int SelectedScreenIndex
        {
            get { return this._SelectedScreenIndex; }
            set
            {
                if (this._SelectedScreenIndex != value)
                {
                    this._SelectedScreenIndex = value;
                    this.OnPropertyChanged("SelectedScreenIndex");
                }
            }
        }

        #endregion

        private bool _registeredOpenScreen;
        private bool _registeredCloseScreen;

        [Import(typeof (OpenScreenEvent), RequiredCreationPolicy = CreationPolicy.Shared, AllowDefault = true,
            AllowRecomposition = true)]
        internal OpenScreenEvent OpenScreen { get; set; }

        [Import(typeof (CloseScreenEvent), RequiredCreationPolicy = CreationPolicy.Shared, AllowDefault = true,
            AllowRecomposition = true)]
        internal CloseScreenEvent CloseScreen { get; set; }

        public ActiveScreensViewModel()
        {
            this.readOnlyScreens = new ReadOnlyObservableCollection<IScreen>(this.screens);
            this.Locator.CompositionProvider.Compose(this);
            //if (DesignerProperties.IsInDesignTool)
            //{
            this.screens.Add(new CaseScreenViewModel());
            this.screens.Add(new CaseScreenViewModel());
            //}
        }


        public void OnImportsSatisfied()
        {
            if (!this._registeredOpenScreen && this.OpenScreen != null)
            {
                this._registeredOpenScreen = true;
                this.OpenScreen.Subscribe(OnOpenScreen);
            }
            if (!this._registeredCloseScreen && this.CloseScreen != null)
            {
                this._registeredCloseScreen = true;
                this.CloseScreen.Subscribe(OnCloseScreen);
            }
            //if (this._registeredCloseScreen && this._registeredOpenScreen )
            //{
            //    this.OpenScreen.Publish(new OpenScreenPayload(null,new CaseScreenDefinition()));
            //}
        }

        /// <summary>
        ///   this is used to remove an instance of a screen from the tabs that are being displayed
        /// </summary>
        /// <param name = "payload"></param>
        public void OnCloseScreen(IScreen payload)
        {
            if (payload == null)
                throw new NullReferenceException("Can't close a null screen that is a silly thing to do :-)");
            this.screens.Remove(payload);
        }

        public void OnOpenScreen(OpenScreenPayload payload)
        {
            IScreen screen = null;
            if (payload.ScreenDefinition.AllowManyInstances)
            {
                // many instances are allowed so lets just create and display the screen 
                try
                {
                    screen = payload.ScreenDefinition.CreateInstanceOfScreen.Invoke(payload.Arguments);
                    if (screen == null) return;
                    this.Locator.CompositionProvider.Compose(screen);
                    int newIndex = this.screens.Count;
                    this.screens.Add(screen);
                    this.SelectedScreenIndex = newIndex;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            else
            {
                // we can only have one active instance of a screen
            }
        }
    }
}